﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for KPIAchievementTableSerializer
    /// </summary>
    public class KPIAchievementTableSerializer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetAllData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string[] Columns
        {
            get
            {
                return new string[]
                {
                   "Jan",
                   "Feb",
                   "Mar",
                   "Apr",
                   "May",
                   "Jun",
                   "Jul",
                   "Aug",
                   "Sep",
                   "Oct",
                   "Nov",
                   "Dec"
                };
            }
        }

        private string GetAllData()
        {
            int sEcho = Convert.ToInt16(HttpContext.Current.Request.Params["sEcho"]);
            int iDisplayLength = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayLength"]);
            int iDisplayStart = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayStart"]);
            int iSortCol_0 = Convert.ToInt16(HttpContext.Current.Request.Params["iSortCol_0"]);
            string sSortDir = HttpContext.Current.Request.Params["sSortDir_0"];
            string sSearch = HttpContext.Current.Request.Params["sSearch"];
            string sSortColumn = Columns[iSortCol_0];
            int year = Convert.ToInt32(HttpContext.Current.Request.Params["Year"]);
            int KPIAssignID = Convert.ToInt32(HttpContext.Current.Request.Params["KPIAssign"]);

            IList<KPIAchievement> result = new List<KPIAchievement>();
            KPIAchievementFacade pf = new KPIAchievementFacade();
            int iTotalRecords = 1;
            result = pf.GetKPIAchievementTable(year, KPIAssignID);
            int iTotalDisplayRecords = 1;

            iTotalDisplayRecords = iTotalRecords;

            IDictionary<int, IDictionary<string, string>> collection = new Dictionary<int, IDictionary<string, string>>();

            if (result.Count > 0)
            {
                IDictionary<string, string> ColumnValues = new Dictionary<string, string>();
                ColumnValues.Add(this.Columns[0], result[0] == null ? "False" : (result[0].StatusByUser == null ? "False" : (result[0].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[1], result[1] == null ? "False" : (result[1].StatusByUser == null ? "False" : (result[1].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[2], result[2] == null ? "False" : (result[2].StatusByUser == null ? "False" : (result[2].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[3], result[3] == null ? "False" : (result[3].StatusByUser == null ? "False" : (result[3].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[4], result[4] == null ? "False" : (result[4].StatusByUser == null ? "False" : (result[4].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[5], result[5] == null ? "False" : (result[5].StatusByUser == null ? "False" : (result[5].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[6], result[6] == null ? "False" : (result[6].StatusByUser == null ? "False" : (result[6].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[7], result[7] == null ? "False" : (result[7].StatusByUser == null ? "False" : (result[7].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[8], result[8] == null ? "False" : (result[8].StatusByUser == null ? "False" : (result[8].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[9], result[9] == null ? "False" : (result[9].StatusByUser == null ? "False" : (result[9].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[10], result[10] == null ? "False" : (result[10].StatusByUser == null ? "False" : (result[10].StatusByVerificator == null ? "Wait" : "True")));
                ColumnValues.Add(this.Columns[11], result[11] == null ? "False" : (result[11].StatusByUser == null ? "False" : (result[11].StatusByVerificator == null ? "Wait" : "True")));
                collection.Add(1, ColumnValues);
            }

            IList<IDictionary<string, string>> aaData = collection.Values.ToList();

            var gridData = new
            {
                sEcho,
                iTotalRecords,
                iTotalDisplayRecords,
                aaData
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }
    }
}